package com.zjun.demo.experimentaldemo.honeycomb

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.zjun.demo.experimentaldemo.R


/**
 * 颜色显示控件
 */
class ColorDisplayView : View {

    private var mStrokeWidth: Float = 0f

    private var mStrokeColor = Color.WHITE

    private var mColorPaint: Paint = Paint()

    private var mColor: Int = Color.GREEN

    private var mInterval: Float = 5f

    private var mIsSelected: Boolean = false

    constructor(context: Context) : super(context) {
        initialization()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initialization(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initialization(attrs)
    }

    @SuppressLint("ResourceType")
    private fun initialization(attrs: AttributeSet? = null) {
        mColorPaint.run {
            isAntiAlias = true
        }
        attrs?.run {
            val arrtArray = intArrayOf(
                R.attr.interval,
                R.attr.cd_stroke_width,
                R.attr.cd_stroke_color,
                R.attr.cd_color,
                R.attr.cd_is_selected,
            )
            val typedArray = context.obtainStyledAttributes(
                attrs,
                arrtArray
            )
            typedArray.let {
                mInterval = it.getDimension(0, dip2px(6f))
                mStrokeWidth = it.getDimension(1, dip2px(1f))
                mStrokeColor = it.getColor(2, Color.WHITE)
                mColor = it.getColor(3, Color.GREEN)
                mIsSelected = it.getBoolean(4, false)
            }
            typedArray.recycle()
        }
    }

    /**
     * 更新颜色
     */
    fun updateColor(color: Int) {
        if(mColor!=color){
            mColor = color
            invalidate()
        }
    }

    /**
     * 是否显示光环
     */
    fun updateIsSelected(isSelected: Boolean) {
        if(mIsSelected!=isSelected){
            mIsSelected = isSelected
            invalidate()
        }
    }

    /**
     * 获取光环是否已经显示
     */
    fun isHasSelected() = mIsSelected

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (mIsSelected) {
            val circleX = (width - mStrokeWidth / 2f) / 2f - 2
            mColorPaint.reset()
            mColorPaint.color = mStrokeColor
            mColorPaint.strokeWidth = mStrokeWidth
            mColorPaint.style = Paint.Style.STROKE
            canvas.drawCircle(width / 2f, width / 2f, circleX, mColorPaint)
        }

        mColorPaint.reset()
        mColorPaint.color = mColor
        mColorPaint.style = Paint.Style.FILL
        val circleX1 = (width - mStrokeWidth / 2f) / 2f - mInterval
        canvas.drawCircle(width / 2f, width / 2f, circleX1, mColorPaint)
    }

    /**
     * dp转px
     * @param dipValue dp值
     */
    private fun dip2px(dipValue: Float): Float {
        val scale: Float = context.resources.displayMetrics.density
        return dipValue * scale + 0.5f
    }

}